/*================ dbs.c ======================*/ 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dbs.h"


/* Create a new server struct */
dbs create_dbs(const char * name, const char * url, int port){
	dbs server = calloc(1, sizeof(struct _dbs));

	server->name = calloc(strlen(name)+1, sizeof(char));
	server->url = calloc(strlen(url)+1, sizeof(char));
	server->port = port;
	server->state = 0;
	server->label = 0;

	strcpy(server->name, name);
	strcpy(server->url, url);

	return server;
}


/* delete a server struct */
void delete_dbs(dbs *server){
   	free((*server)->name);
   	free((*server)->url);
	free(*server);
	*server = NULL;
}

/* print to stdout a server struct */
void print_dbs(const dbs server){
	if(server != NULL){
		if (server->name != NULL || server->url != NULL){
			printf("Server name -%s\n", server->name);
			printf("Server url  -%s\n", server->url);
			printf("Server port -%d\n", server->port);
		}
			else 
				printf("Erro: NULL server member\n");
	}
	else 
		printf("Erro: NULL Server\n");
}



dbs find_dbs(dbs list[], int max_size, const char* new_name){
	int i = 0;
	for(i = 0; list[i] !=NULL && i < max_size ; ++i){
		if ((strcmp((list[i])->name, new_name)) == 0){
			return list[i];
		}
	}
	return NULL;
}


